/*
 * @Descripttion:
 * @version:
 * @Author: kl2018
 * @Date: 2019-12-27 17:46:30
 * @LastEditors: kl2018
 * @LastEditTime: 2020-10-26 10:51:19
 */
import { WeaTableNew } from 'comsMobx';
import { WeaTools } from 'ecCom';
import { observable, action } from 'mobx';

const { TableStore } = WeaTableNew;

export default class BaseTableStore {
  @observable tableUrl = '';
  @observable tableStore = new TableStore();
  @observable selectedRowKeys = [];
  @observable selecteDatas = [];
  @observable tableParams = {}
  @observable tableListNumber = -1;
  @observable selectedKey = '';
  @observable hasRight = true;
  sessionkey = '' // 导出数据时使用

  @action.bound
  /**
   * @name: getTableDatas
   * @test: 获取列表数据
   * @msg:
   * @param {
  *     params : object, //传入的参数
  *     onlyParams : boolean, //是否只使用参入参数
  *     covered : boolean //是否传入参数覆盖
  *     saveParams : boolean, //保存参数参数
   *  }
   * @return:
   */
  getTableDatas (params = {}, onlyParams = true, covered = true, saveParams = true) {
    if (this.tableUrl === '') {
      return;
    }
    if (!onlyParams) {
      if (covered) {
        params = { ...params, ...this.tableParams };
      } else {
        params = { ...this.tableParams, ...params };
      }
    }
    if (saveParams) {
      this.tableParams = params;
    }
    WeaTools.callApi(this.tableUrl, 'POST', params).then((data) => {
      this.hasRight = data.hasRight != null || data.hasRight != undefined ? data.hasRight :true;
      if (this.hasRight && data.sessionkey) { 
        this.sessionkey = data.sessionkeyNoUser || data.sessionkey;
        this.selectedRowKeys = [];
        this.selecteDatas = [];
        if (this.tableListNumber == -1) {
          this.tableStore.getDatas(data.sessionkey, 1);
        } else {
          this.tableStore.getDatas(data.sessionkey, 1, this.tableListNumber);
        }
      }
    });
  }

  @action.bound
  setMobxState (params = {}) {
    Object.keys(params).map((key) => {
      this[key] = params[key];
    });
  }

  @action.bound
  tableOnSelect (selectedRowKeys, selectedRows) {
    this.selectedRowKeys = selectedRowKeys;
    this.selecteDatas = selectedRows;
  }

  @action.bound
  clearSelectKey () {
    this.selectedRowKeys = [];
    this.selecteDatas = [];
    this.tableStore.setSelectedRowKeys([]);
  }

  @action.bound
  init () {
    // this.tableUrl = '';
    this.selectedRowKeys = [];
    this.selecteDatas = [];
    this.tableParams = {};
    this.tableStore = new TableStore();
    this.tableListNumber = -1;
    this.hasRight = true;
  }
}
